package com.googlecode.restitory.gae.http;

import com.googlecode.restitory.gae.filter.util.JsonUtil;

public class RequestAdapter implements RequestService {

	private final RequestService service;

	public RequestAdapter(RequestService service) {
		this.service = service;
	}

	public RequestService getService() {
		return service;
	}

	@Override
	public Response execute(Request req) {
		RequestService service = this.service;
		return service.execute(req);
	}

	public Integer code(String type, String path) {
		return execute(new Request(Type.valueOf(type), path)).getCode();
	}

	public Response createIfNotExists(String path, String contentType, String content) {
		if (code("GET", path) == 404) {
			return execute(new Request(Type.PUT, path).setContentType(contentType).setContent(content));
		}
		return null;
	}

	public String getJson(String path) {
		Response resp = execute(new Request(Type.GET, path));
		if (resp.getCode() == 404) {
			return null;
		}
		if (resp.getCode() != 200) {
			throw new RuntimeException("error: " + resp);
		}
		Content content = resp.getContent();
		return JsonUtil.parse(content.getChars()).getAsString();
	}

	public boolean exists(String path) {
		return execute(new Request(Type.GET, path)).getCode() == 200;
	}

	public Response execute(String type, String path, String contentType, String content) {
		Request req = new Request(Type.valueOf(type), path);
		if (contentType != null) {
			req.setContentType(contentType).setContent(content);
		}
		return execute(req);
	}

	public Response success(String type, String path, String contentType, String content) {
		Response resp = execute(type, path, contentType, content);
		if (resp.getCode() != 200) {
			throw new RuntimeException("error: " + resp);
		}
		return resp;
	}

	public Integer code(String type, String path, String ct, String c) {
		return execute(new Request(Type.valueOf(type), path).setContentType(ct).setContent(c)).getCode();
	}

	public void setUser(String user, String pass) {
		((HttpClientRequestService) service).setUser(user, pass);
	}

	public Content content(String type, String path) {
		Response resp = execute(type, path, null, null);
		if (resp.getCode() != 200) {
			throw new RuntimeException("error: " + resp);
		}
		return resp.getContent();
	}

	public Response success(Request request) {
		Response resp = execute(request);
		if (resp.getCode() != 200) {
			throw new RuntimeException("error: " + resp);
		}
		return resp;
	}

}
